package com.example.remote;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import com.example.musicplayservice.R;

import java.io.FileDescriptor;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;

public class MusicPlayerService extends Service {

    String tag = "***";

    public static MediaPlayer mPlayer;

    private int currentPlaying = 0; // 用作 ArrayList 下标，当前播放的歌曲
    private ArrayList<Integer> playList = new ArrayList<>();
    private ArrayList<String> nameList = new ArrayList<>();

    private boolean isPausing = false, isPlaying = false; // 音乐暂停状态，音乐第一次播放之后变为true

    private  IBinder stub =  new IMusicPlayerService.Stub() {

//        @Override
//        public void play() throws RemoteException {
//            if (isPlaying) {
//                mPlayer.stop();
//                mPlayer.reset();
//            }
//            mPlayer = MediaPlayer.create(getApplicationContext(), playList.get(currentPlaying));
//
//            //        mPlayer.reset();
//     //       mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.qingming);
//
//            mPlayer.start();
//        }

//        @Override
//        public void play() throws RemoteException {
//
//        }
//
        @Override
        public void pause() throws RemoteException {
            mPlayer.pause();
        }
//
        @Override
        public void resume() throws RemoteException {
            mPlayer.start();
        }
//
//
        @Override
        public void stop() throws RemoteException {
            mPlayer.stop();
        }

        @Override
        public int getDuration() throws RemoteException {
            return mPlayer.getDuration();
        }

        @Override
        public int getCurrentPosition() throws RemoteException {
            return mPlayer.getCurrentPosition();
        }

        @Override
        public void seekTo(int current) throws RemoteException {
            mPlayer.seekTo(current);
        }

        @Override
        public boolean setLoop(boolean loop) throws RemoteException {
            return false;
        }

        @Override
        public String setToNext(){
            Log.i("INFO", "onClick: 切歌按钮被点击！");
            currentPlaying++;
            if(currentPlaying==playList.size())
                currentPlaying=0;
            prepareMedia();
            isPausing = false;
            isPlaying = true;

            return nameList.get(currentPlaying);
        }

        @Override
        public String setToPrev(){
            // 重播、上切
            Log.i("INFO", "onClick: 重播按钮被点击！");
       //     playPauseBtn.setImageResource(R.drawable.control_pause_dark); // 切换成暂停键 写在MainActivity里面
            if (!mPlayer.isPlaying()) {  //如果没有被暂停，就不切歌
//                currentPlaying = --currentPlaying % playList.size();
                currentPlaying--;
                if(currentPlaying==-1)
                    currentPlaying=playList.size()-1;

            }
            prepareMedia();
            isPausing = false;
            isPlaying = true;
            return nameList.get(currentPlaying);

        }

        @Override
        public int playOrPause(){
            int flag=0;
            // 开始暂停
            Log.i("INFO", "onClick: 开始暂停按钮被点击！");
            if (!isPausing && !isPlaying) { // 暂停状态，且从未被播放
                // 开始播放
              //  playPauseBtn.setImageResource(R.drawable.control_pause_dark); // 切换成暂停键  在MainActivity里面
                prepareMedia();
                isPlaying = true;
                flag= 1;
            } else if (isPausing && isPlaying) { // 暂停状态，且被播放过一次
                // 继续播放
           //     playPauseBtn.setImageResource(R.drawable.control_pause_dark); // 切换成暂停键
                mPlayer.start();
                flag= 2;
            } else { // 播放状态
                // 暂停播放
              //  playPauseBtn.setImageResource(R.drawable.control_play_dark); // 切换成播放键
                mPlayer.pause();
                flag=3;
            }
            isPausing = !isPausing; // 切换状态
            return flag;
        }

        @Override
        public String getSongName() throws RemoteException {
            return nameList.get(currentPlaying);
        }

    };

    @Override
    public void onCreate() {
        Log.i(tag, "MusicService onCreate()");
        //mPlayer =new MediaPlayer();
        preparePlaylist();

    }



    @Override
    public IBinder onBind(Intent intent) {
        if(stub!=null)
            Log.e("wocao*******","*********************************");
        return stub;
    }

    @Override
    public void onDestroy() {
        if (mPlayer != null) {
            mPlayer.release();
        }
        super.onDestroy();
    }

    private void preparePlaylist() {
        Field[] fields = R.raw.class.getFields();
        for (int count = 0; count < fields.length; count++) {
            Log.i("Raw Asset", fields[count].getName());
            try {
                int resId = fields[count].getInt(fields[count]);
                String resName=fields[count].getName();
                nameList.add(resName);
                playList.add(resId);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    private void prepareMedia() {
        if (isPlaying) {
            mPlayer.stop();
            mPlayer.reset();
        }
        mPlayer = MediaPlayer.create(getApplicationContext(), playList.get(currentPlaying));
        mPlayer.start();

    }
    @Override
    public boolean onUnbind(Intent intent) {

                 Log.i("TAG","service onUnbind");
                 return super.onUnbind(intent);
             }

}
